<?php

namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller{
	//用户登陆页面
	public function login() {
		$this->display();
	}
	public function checkLogin() {
		if(IS_AJAX) {
			$db = D('User');
			if( !$db->create() ) {
				$this->error( $db->getError() );
			}else{
				$user = $db->where( array('username' => I('post.username')) )->find();
                if( !$user ) {
                	$this->error('用户名不存在！');
                }
                if( $user['password'] !== I('post.password','','md5') ) {
                	$this->error('用户名或者密码错误！');
                }
                if( $user['status'] ) {
                	$this->error('你的账号已被锁定，请联系管理员！');
                }
				session('uid', $user['id']);
				session('username', $user['username']);
				session('user', $user);
				$data = array(
		            'logintimes'           => array('exp', '`logintimes`+1'),
		            'last_login_time' => time(),
		            'last_login_ip'   => get_client_ip(),
				);
				$db->data($data)->where(array('id'=>$user['id']))->save();
				$this->success('登录成功！', U('Index/index'));
			}
		}else{
			E('非法操作！');
		}
	}
    //用户登出
	public function logout() {
		session(null);
		session_destroy();
		redirect( U('Public/login') );
	}
}